package com.howells.time.module.personal.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.howells.time.R;
import com.howells.time.base.activity.BaseActivity;
import com.howells.time.base.utils.FileUtils;
import com.howells.time.base.utils.SharedPrefsUtil;
import com.howells.time.base.utils.StatusBarUtil;
import com.howells.time.base.utils.ToastShow;
import com.howells.time.module.personal.presenter.UpdateInfoPresenter;
import com.howells.time.module.personal.presenter.UpdateInfoPresenterImp;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016.4.7.
 */
public class UpdateInfoActivity extends BaseActivity implements UpdateInfoView{
    private UpdateInfoPresenter updateInfoPresenter;
    private SelectPicWindows selectPicWindows;
    private EditText name;
    private EditText area;
    private EditText sign;
    private RadioButton man;
    private RadioButton woman;

    private static final String IMAGE_FILE_NAME = "touxiang.png";// 头像文件名称
    private static final int REQUESTCODE_PICK = 0;		// 相册选图标记
    private static final int REQUESTCODE_TAKE = 1;		// 相机拍照标记
    private static final int REQUESTCODE_CUTTING = 2;	// 图片裁切标记
    private String urlpath;			// 图片本地路径
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        updateInfoPresenter=new UpdateInfoPresenterImp(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        StatusBarUtil.setColor(this,0);
        init();

    }

    private void init() {
        name= (EditText) findViewById(R.id.setting_info_edit_name);
        area= (EditText) findViewById(R.id.setting_info_edit_area);
        sign= (EditText) findViewById(R.id.setting_info_edit_sign);
        man= (RadioButton) findViewById(R.id.setting_info_radio_man);
        woman= (RadioButton) findViewById(R.id.setting_info_radio_woman);
        name.setText(SharedPrefsUtil.getValue(this, "UserInfo", "name", ""));
        area.setText(SharedPrefsUtil.getValue(this, "UserInfo", "address", ""));
        sign.setText(SharedPrefsUtil.getValue(this, "UserInfo", "autograph", ""));
        if(SharedPrefsUtil.getValue(this,"UserInfo","sex","").equals("1")){
            woman.setChecked(true);
        }else {
            man.setChecked(true);
        }
        ((TextView)findViewById(R.id.top_pannel_title)).setText("个人信息");
    }

    public void saveinfo(View view){
        Map<String,String> info=new HashMap<>();
        info.put("name",name.getText().toString());
        info.put("address",area.getText().toString());
        info.put("autograph", sign.getText().toString());
        if(woman.isChecked()){
            info.put("sex","1");
        }else {
            info.put("sex","0");
        }
        updateInfoPresenter.updateinfo(info);
    }

    public void onClick(View view){
        switch (view.getId()){
            case R.id.top_pannel_back:
                finish();
                break;
        }
    }

    public void selectphoto(View view){
        selectPicWindows=new SelectPicWindows(this,itemsOnClick);
        selectPicWindows.showAtLocation(findViewById(R.id.me_setting_layout), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }


    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectPicWindows.dismiss();
            switch (v.getId()) {
                // 拍照
                case R.id.btn_take_photo:
                    Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    //下面这句指定调用相机拍照后的照片存储的路径
                    takeIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME)));
                    startActivityForResult(takeIntent, REQUESTCODE_TAKE);
                    break;
                // 相册选择图片
                case R.id.btn_pick_photo:
                    Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
                    // 如果朋友们要限制上传到服务器的图片类型时可以直接写如："image/jpeg 、 image/png等的类型"
                    pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(pickIntent, REQUESTCODE_PICK);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUESTCODE_PICK:// 直接从相册获取
                try {
                    startPhotoZoom(data.getData());
                } catch (NullPointerException e) {
                    e.printStackTrace();// 用户点击取消操作
                }
                break;
            case REQUESTCODE_TAKE:// 调用相机拍照
                File temp = new File(Environment.getExternalStorageDirectory() + "/" + IMAGE_FILE_NAME);
                startPhotoZoom(Uri.fromFile(temp));
                break;
            case REQUESTCODE_CUTTING:// 取得裁剪后的图片
                if (data != null) {
                    setPicToView(data);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    /**
     * 裁剪图片方法实现
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, REQUESTCODE_CUTTING);
    }


    /**
     * 保存裁剪之后的图片数据
     * @param picdata
     */
    private void setPicToView(Intent picdata) {
        Bundle extras = picdata.getExtras();
        if (extras != null) {
            // 取得SDCard图片路径做显示
            Bitmap photo = extras.getParcelable("data");
            urlpath = FileUtils.saveFile("touxiang.png", photo);

            // 新线程后台上传服务端
            updateInfoPresenter.upload(urlpath);
        }
    }

    @Override
    public void updatesuccess(Map<String,String> info) {
        finish();
        ToastShow.ToastShort(this, "修改成功");
        SharedPrefsUtil.putValue(this, "UserInfo", "name", info.get("name"));
        SharedPrefsUtil.putValue(this, "UserInfo", "address",info.get("address"));
        SharedPrefsUtil.putValue(this, "UserInfo", "autograph",info.get("autograph"));
        SharedPrefsUtil.putValue(this, "UserInfo", "sex",info.get("sex"));
    }
}
